

# 1. replace : X_oneshot, Y_oneshot, Y, X0 in the template
from pathlib import Path

p_t = Path.cwd() / '10a-XY-template.md'

p_Xo = Path.cwd() / '10a-default-oneshot.X.md'
p_Yo = Path.cwd() / '10a-default-oneshot.Y.yml'

p_X0 = Path.cwd() / 'XY'/ '00-02.X0.md'
p_Y = Path.cwd() / 'XY' / '00-02.Y.yml'

n = p_Y.name.removesuffix('Y.yml')
p_X = p_Y.with_name(f"{n}X.md")

# 2. replace the template with the oneshot and Y
s = p_t.read_text()
s1 = s.format(
    X_oneshot=p_Xo.read_text(),
    Y_oneshot=p_Yo.read_text(),
    Y=p_Y.read_text(),
    X0=p_X0.read_text()
)
s1

# 3. ask AI
from u import Sess, U
import json
p_qa = Path.cwd().parent / "ixc-qa"
k = json.loads(Path("/home/me/.ssh/k-250611-h3c.json").read_text())
s = Sess.new(model="google/gemini-2.5-flash-preview-05-20:thinking", url=k['url'], key=k['key'])

s.add_q(s1); await s.ask_for_a(); None


def extract_code_block(text, lang:str = ''):
    import re
    pattern = r'```(.*?)\n(.*?)\n```'
    match = re.search(pattern, text, re.DOTALL)
    if lang:
        assert match.group(1).strip() == lang, f"Expected language {lang}, but found {match.group(1).strip()}"
    if match:
        return match.group(2).strip()
    raise ValueError(f"No code block found in the text.")
o = s.ms[-1].content
# o = U.extract_code_block(o, 'markdown')
o = extract_code_block(o, 'markdown')
p_X.write_text(o)

# --------------------------------------------------

# --------------------------------------------------
